package com.upcode.weighing.admin.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.domain.SysArea;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.service.SysAreaService;
import com.upcode.weighing.util.HttpsUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/area")
@Api(value = "/area", description = "区域接口", produces = MediaType.ALL_VALUE)
public class SysAreaController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysAreaController.class);

    @Autowired
    private SysAreaService sysAreaService;

    @OpsLog(operation = "区域接口/新增" ,type = 2)
    @GetMapping("/addArea")
    public Object addArea(){
        String url="https://restapi.amap.com/v3/config/district?subdistrict=3&key=c21484f46a8c70d96c2156ae529f214f";
        JSONObject jsonObject= HttpsUtil.httpRequest(url,"GET",null);
        JSONArray countryAll = jsonObject.getJSONArray("districts");
        for(int i=0;i<countryAll.size();i++){
            //国家
            JSONObject countryLeve0 = countryAll.getJSONObject(i);
//            String adcode0 = countryLeve0.getString("adcode");
//            String citycode0 = countryLeve0.getString("citycode");
//            String name0 = countryLeve0.getString("name");
//            String center0 = countryLeve0.getString("center");
//            String country = countryLeve0.getString("level");
//            int level=0;
//            if(country.equals("country")){
//                level=0;
//            }
//            //插入国家
//            String id0= insert("0",adcode0,citycode0,name0,center0,level);
            //省
            JSONArray provinceAll = countryLeve0.getJSONArray("districts");
            for(int j=0;j<provinceAll.size();j++){
                JSONObject province0 = provinceAll.getJSONObject(j);
                String adcode1 = province0.getString("adcode");
                String citycode1 = province0.getString("citycode");
                String name1 = province0.getString("name");
                String center1 = province0.getString("center");
                String province = province0.getString("level");
                int level1=0;
                if(province.equals("province")){
                    level1=1;
                }
                //插入省
                Long id1=insert(0L,adcode1,citycode1,name1,center1,level1);
                //市
                JSONArray cityAll = province0.getJSONArray("districts");
                for(int k=0;k<cityAll.size();k++){
                    JSONObject city0 = cityAll.getJSONObject(k);
                    String adcode2 = city0.getString("adcode");
                    String citycode2 = city0.getString("citycode");
                    String name2 = city0.getString("name");
                    String center2 = city0.getString("center");
                    String city = city0.getString("level");
                    int level2=0;
                    if(city.equals("city")){
                        level2=2;
                    }
                    if(city.equals("district")){
                        level2=3;
                    }
                    //插入市
                    Long id2=insert(id1,adcode2,citycode2,name2,center2,level2);
                    //县区
                    JSONArray districtAll = city0.getJSONArray("districts");
                    for(int l=0;l<districtAll.size();l++){
                        JSONObject district0 = districtAll.getJSONObject(l);
                        String adcode3 = district0.getString("adcode");
                        String citycode3 = district0.getString("citycode");
                        String name3 = district0.getString("name");
                        String center3 = district0.getString("center");
                        String district = district0.getString("level");
                        int level3=0;
                        if(district.equals(district)){
                            level3=3;
                        }
                        insert(id2,adcode3,citycode3,name3,center3,level3);
                    }
                }
            }
        }
        return success("新增成功");
    }
    //公共插入方法
    public Long insert(Long parentId,String adcode, String citycode, String name, String center, int level) {
        SysArea a=new SysArea();
        a.setId(IdWorker.getId());
        a.setAdcode(adcode);
        if(!citycode.equals("[]")){
            a.setCitycode(citycode);
        }else{
            a.setCitycode("无");
        }
        a.setLevel(level);
        a.setName(name);
        a.setParentId(parentId.toString());
        String[] lonLat=center.split(",");
        a.setLongitude(lonLat[0]);
        a.setLatitude(lonLat[1]);
        sysAreaService.addArea(a);
        return a.getId();
    }
}
